// JavaScript Document
var STSingleTimer = {};
// private
STSingleTimer.__private__ = {};
STSingleTimer.__private__.pool = {};
STSingleTimer.__private__.intervalIds = {};
STSingleTimer.__private__.getUpdater = function( delay )
{
	return function(){
							var _p,_delay;
							var _listeners;
							
							_delay = delay;
							_p = STSingleTimer.__private__;
							_listeners = _p.pool[ _delay ];
							if( _p.pool[ _delay ] == undefined || _listeners.length == 0 )
							{
								STSingleTimer.stop( _delay );
								return;
							}
							for( var i in _listeners )
							{
								_listeners[ i ].apply( null );
							}
						};
}



// API
STSingleTimer.addListener = function( voidFn, delay )
{
	if( delay == undefined ) delay = STICKER.SINGLE_TIMER_DELAY;
	
	var _p = STSingleTimer.__private__;
	if( _p.pool[ ""+delay ] == undefined ) _p.pool[ ""+delay ] = [];
	var _listeners;
	_listeners = _p.pool[ ""+delay ];
	
	var _idx = _listeners.indexOf( voidFn );
	if( _idx < 0 )
	{
		_listeners.push( voidFn );
		this.start( delay );
	}
}
STSingleTimer.removeListener = function( voidFn )
{
	var _p = STSingleTimer.__private__;
	var _listeners;
	for( var delay in _p.pool )
	{
		_listeners = _p.pool[ delay ];
		var _idx = _listeners.indexOf( voidFn );
		if( _idx > -1 )
		{
			_listeners.splice( _idx, 1 );
			if( _listeners.length == 0 )
			{
				delete _p.pool[ delay ];
				STSingleTimer.stop( delay );
			}
		}
	}
}
STSingleTimer.start = function( delay )
{
	var _p,_key;
	_p = STSingleTimer.__private__;
	_key = ""+delay;
	if( _p.intervalIds[ _key ] != undefined ) return;
	_p.intervalIds[ _key ] = setInterval( _p.getUpdater( delay ), delay );
}

STSingleTimer.stop = function( delay )
{
	var _p,_key;
	_p = STSingleTimer.__private__;
	_key = ""+delay;
	if( _p.intervalIds[ _key ] == undefined ) return;
	clearInterval( _p.intervalIds[ _key ] );
	delete _p.intervalIds[ _key ];
}
